package com.zhangzy.qm.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;

import com.zhangzy.qm.entity.Ablum;
import com.zhangzy.qm.entity.History;

public class AblumDao extends BaseDao<Ablum> {

	public void  add(Ablum ab){
		super.getHibernateTemplate().save(ab);
		int i=0;
	}
	
	/**
	 * 查询
	 * @Methods Name query
	 * @Create In May 1, 2012 By musicbear
	 * @param sql
	 * @return List<Ablum>
	 */
	public List<Ablum> query(String sql){
		Session session = getSession();
		Query query = session.createQuery(sql);
		List<Ablum> list = query.list();
		session.close();	
		return list;
	}
	public List<Ablum> query(String sql,int start,int pageSize){
		Session session = getSession();
		Query query = session.createQuery(sql);
		query.setFirstResult(start);
		query.setMaxResults(pageSize);		
		List<Ablum> list = query.list();
		session.close();	
		return list;
	}
	
	/**
	 * 查总数
	 * @Methods Name queryCount
	 * @Create In May 1, 2012 By zhenyu
	 * @param sql
	 * @return int
	 */
	public int queryCount(String sql){
		Session session = getSession();
		Query query = session.createQuery(sql);
		Long count = (Long) query.uniqueResult();
		session.close();	
		return count.intValue();	
	}
	
	public void update(Ablum ab){
		super.getHibernateTemplate().update(ab);
	}
	
	public Ablum getAblum(int id){
		return (Ablum) super.getHibernateTemplate().get(Ablum.class, id);
	}
	
	public boolean isExist(String hql){
		boolean flag = false;
		try {
			Session session = getSession();
			Query query = session.createQuery(hql);
			Long count = (Long) query.uniqueResult();
			if(count>0){
				flag=true;
			}			
			session.close();	
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return flag;	
	}
}
